from manim import *
class math(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text = Tex(r"\text{设函数}$y=f\left( x\right) $\text{由}$\left\{\begin{matrix}x=2t+\left| t\right| \\y=\left| t\right| \sin t\end{matrix}\right.$\text{,确定,则}"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-17.5,2.5,0])
        
        
        text2 = Tex(r"$\left( A\right) f\left( x\right) $\text{连续,}$f'\left( 0\right)  $\text{不存在.}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,-5,0])
        text3 = Tex(r"$\left( B\right) f'\left( 0\right) $\text{存在,}$f'\left( x\right) $\text{在x=0处不连续.}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,-5,0])
        text4 = Tex(r"$\left( C\right) f'\left( x\right) $\text{连续,}$f''\left( 0\right) $\text{不存在.}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,-10,0])
        text5 = Tex(r"$\left( D\right) f''\left( 0\right) $\text{存在,}$f''\left( x\right)$\text{在x=0处不连续.}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,-10,0])
        self.play(Write(text),Write(text2),Write(text3),Write(text4),Write(text5),self.camera.frame.animate.scale(0.8))
        self.wait()
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text.animate().move_to([-17.5,22.5,0]) 
                  ,text2.animate().move_to([-25,15,0]) ,text3.animate().move_to([25,15,0])
                   ,text4.animate().move_to([-25,10,0]) ,text5.animate().move_to([25,10,0]) )
        rectangle=Rectangle(color=BLUE,fill_opacity=0.3,width=90,height=10
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([3, 12.5, 0])
        self.play(Create(rectangle))
        self.play(FadeOut(rectangle))
        rectangle1=Rectangle(color=RED,fill_opacity=0.3,width=16,height=7
                            ,stroke_color=RED,stroke_width=20)
        rectangle1.move_to([-12.5, 22.5, 0])
        rectangle2=Rectangle(color=RED,fill_opacity=0.3,width=16,height=7
                            ,stroke_color=RED,stroke_width=20)
        rectangle2.move_to([-12.5, 22.5, 0])
        self.play(Create(rectangle1),Create(rectangle2))

        text6 = Tex(r"\text{当}$t\geq 0$\text{时,}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,0,0])
        self.play(Write(text6))
        text7 = Tex(r"$\left\{\begin{matrix}x=3t \\y=t\sin t\end{matrix}\right.$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,0,0])
        self.play(ReplacementTransform(rectangle1,text7))
        self.wait(0.2)

        arrow = Arrow(start=[-2,0,0],end=[3,0,0],color=RED
                       , stroke_width=20, tip_length=1)
        self.play(GrowArrow(arrow))


        text8 = Tex(r"$y=\dfrac{x}{3}\sin \dfrac{x}{3}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([11,0,0])
        self.play(Write(text8))


        rectangle3=Rectangle(color=GREEN,fill_opacity=0.3,width=11,height=4
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle3.move_to([-25, 0, 0])
        rectangle4=Rectangle(color=GREEN,fill_opacity=0.3,width=9,height=3
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle4.move_to([-9.5, 2, 0])
        self.play(Create(rectangle4),Create(rectangle3))
        text9 = Tex(r"$,x\geq 0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text8,RIGHT,buff=1)


        self.play(ReplacementTransform(rectangle3,text9),ReplacementTransform(rectangle4,text9))
        self.wait(0.2)
        


        text60 = Tex(r"\text{当}$t< 0$\text{时,}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,-10,0])
        self.play(Write(text60))
        text70 = Tex(r"$\left\{\begin{matrix}x=t \\y=-t\sin t\end{matrix}\right.$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,-10,0])
        self.play(ReplacementTransform(rectangle2,text70))
        self.wait(0.2)

        arrow1 = Arrow(start=[-2,-10,0],end=[3,-10,0],color=RED
                       , stroke_width=20, tip_length=1)
        self.play(GrowArrow(arrow1))


        text80 = Tex(r"$y=-x\sin x$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([11,-10,0])
        self.play(Write(text80))


        rectangle30=Rectangle(color=GREEN,fill_opacity=0.3,width=11,height=4
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle30.move_to([-25, -10, 0])
        rectangle40=Rectangle(color=GREEN,fill_opacity=0.3,width=9,height=3
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle40.move_to([-9.5, -8, 0])
        self.play(Create(rectangle40),Create(rectangle30))
        text90 = Tex(r"$,x< 0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text80,RIGHT,buff=1)


        self.play(ReplacementTransform(rectangle30,text90),ReplacementTransform(rectangle40,text90))

        rectangle300=Rectangle(color=GREEN,fill_opacity=0.3,width=23,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle300.move_to([15, -10, 0])
        rectangle400=Rectangle(color=GREEN,fill_opacity=0.3,width=23,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle400.move_to([15, 0, 0])
        self.play(Create(rectangle400),Create(rectangle300))

        text10 = Tex(r"$f\left( x\right) =\left\{\begin{matrix}\dfrac{x}{3}\sin \dfrac{x}{3}  &,x\geq 0 \\-x\sin x &,x <0\end{matrix}\right.$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,0,0])
        self.play(ReplacementTransform(rectangle300,text10),ReplacementTransform(rectangle400,text10)
                  ,FadeOut(text6,text7,text8,text9,arrow)
                  ,FadeOut(text60,text70,text80,text90,arrow1))

        text11 = Tex(r"$\lim\limits _{x\rightarrow 0^+}f\left( x\right) =\lim\limits _{x\rightarrow 0^{+}}f\left( x\right) =f\left( 0\right) =0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,0,0])
        self.play(Write(text11))
        text12 = Tex(r"\text{(连续)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text11,RIGHT,buff=2)
        self.play(Write(text12))
        text13 = Tex(r"$f_{+}'\left( 0\right) =\lim\limits  _{x\rightarrow 0^{+}}\dfrac{f\left( x\right) -f\left( 0\right) }{x-0}=\lim\limits _{x\rightarrow 0^{+}}\dfrac{1}{3}\sin \dfrac{x}{3}=0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-10,0])
        text14 = Tex(r"$f_{-}'\left( 0\right) =\lim\limits  _{x\rightarrow 0^{-}}\dfrac{f\left( x\right) -f\left( 0\right) }{x-0}=\lim\limits _{x\rightarrow 0^{-}}\left( -\sin x\right) =0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-20,0])
        self.play(Write(text13),Write(text14))
        rectangle5=Rectangle(color=GREEN,fill_opacity=0.3,width=3,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle5.move_to([36, -10, 0])
        rectangle6=Rectangle(color=GREEN,fill_opacity=0.3,width=3,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle6.move_to([37, -20, 0])
        self.play(Create(rectangle5),Create(rectangle6))

        text15 = Tex(r"\text{(可导)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([40,-15,0])
        self.play(ReplacementTransform(rectangle5,text15),ReplacementTransform(rectangle6,text15))
        #半透明矩阵扩展
        rectangle7=Rectangle(color=RED,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=RED,stroke_width=0).move_to([-25,15,0])
        self.play(
            rectangle7.animate.stretch(31.0,dim=0)
        )

        self.wait(0.2)


        text16 = Tex(r"$f'\left( x\right) =\left\{\begin{matrix}\dfrac{1}{3}\sin \dfrac{x}{3}+\dfrac{x}{9}\cos \dfrac{x}{3} & ,x >0\\0 & ,x=0\\-\sin x-x\cos x &,x<0\end{matrix}\right.$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-27,0,0])
        self.play(ReplacementTransform(text10,text16)
                  ,FadeOut(text12,text13,text14,text15,text11))

        text110 = Tex(r"$\lim\limits _{x\rightarrow 0^+}f'\left( x\right) =\lim\limits _{x\rightarrow 0^{+}}f'\left( x\right) =f'\left( 0\right) =0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([18,0,0])
        self.play(Write(text110))
        text120 = Tex(r"\text{(连续)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text110,RIGHT,buff=2)
        self.play(Write(text120))

        rectangle70=Rectangle(color=RED,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=RED,stroke_width=0).move_to([25,15,0])
        self.play(
            rectangle70.animate.stretch(45.0,dim=0)
        )

        self.wait(0.2)


        text130 = Tex(r"$f_{+}''\left( 0\right) =\lim\limits  _{x\rightarrow 0^{+}}\dfrac{f'\left( x\right) -f'\left( 0\right) }{x-0}=\lim\limits _{x\rightarrow 0^{+}}\dfrac{\dfrac{1}{3}\sin \dfrac{x}{3}+\dfrac{x}{9}\cos \dfrac{x}{3}}{x}=\dfrac{2}{9}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        text140 = Tex(r"$f_{-}''\left( 0\right) =\lim\limits  _{x\rightarrow 0^{-}}\dfrac{f'\left( x\right) -f'\left( 0\right) }{x-0}=\lim\limits _{x\rightarrow 0^{-}}\dfrac{-\sin x-x\cos x}{x}=-2$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-20,0])
        self.play(Write(text130),Write(text140))
        rectangle50=Rectangle(color=GREEN,fill_opacity=0.3,width=4,height=6
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle50.move_to([32.5, -11, 0])
        rectangle60=Rectangle(color=GREEN,fill_opacity=0.3,width=4,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle60.move_to([32.5, -20, 0])
        self.play(Create(rectangle50),Create(rectangle60))

        text150 = Tex(r"\text{(不可导)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([40,-15,0])
        self.play(ReplacementTransform(rectangle50,text150),ReplacementTransform(rectangle60,text150))


        rectangle700=Rectangle(color=GREEN,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=GREEN,stroke_width=0).move_to([-25,10,0])
        self.play(
            rectangle700.animate.stretch(31.0,dim=0)
        )

        self.wait(0.2)

        self.wait()